<template>
  <div>
    <el-card class="login-card">
      <div slot="header" class="clearfix">
        <span>登录</span>
        <el-button style="float: right; padding: 3px 0" type="text"
          >注册</el-button
        >
      </div>
      <el-form
        ref="loginFormRef"
        :rules="rules"
        :model="loginForm"
        label-position="right"
        label-width="60px"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            prefix-icon="el-icon-user"
            v-model="loginForm.username"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            prefix-icon="el-icon-goods"
            v-model="loginForm.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="remeber">
          <el-checkbox
            label="记住密码"
            name="type"
            v-model="loginForm.remeber"
          ></el-checkbox>
        </el-form-item>
        <el-button-group class="login-btn">
          <el-button type="primary" plain="" @click="login" :loading="isLoading"
            >登录</el-button
          >
          <el-button type="primary" plain="" @click="reset">重置</el-button>
        </el-button-group>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { api_login } from '@/api/admin'

export default {
  name: 'Login',
  data() {
    return {
      isLoading: false,
      loginForm: {
        username: '',
        password: '',
        remeber: true
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 4, max: 20, message: '长度在 4 到 20 个字符', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    //表单重置
    reset: function() {
      this.$refs['loginFormRef'].resetFields()
    },
    //登录
    login: function() {
      this.isLoading = true
      //1.验证表单
      this.$refs['loginFormRef'].validate(async valid => {
        if (!valid) {
          this.$message.error('请正确填写登录信息！')
          this.isLoading = false
          return false
        }
        //2.提交数据到后台,提交表单
        const { data: res } = await api_login(this.loginForm)
        console.log(res)
        this.isLoading = false
        if (res.code !== 200) {
          this.$message.error(res.message)
          return true
        }
        const data = res.data
        if (!data) return false
        const token = data.tokenHead + data.token
        //3.保存token到sessionStorage
        window.sessionStorage.setItem('token', token) //会话结束后，自动消除
        //4.保存用户信息到vuex中的store
        //5.记住密码功能，登录将用户密码保存到cookies
        if (this.loginForm.remeber) {
          this.$cookies.set('username', this.loginForm.username, '7D') //默认是一天过期
          this.$cookies.set('password', this.loginForm.password, '7D')
        } else {
          this.$cookies.remove('username')
          this.$cookies.remove('password')
        }
        //6.登录页面刷新或重新加载的时候从cookies中获取,在created函数中
        //7.跳转到主页,编程式路由跳转
        await this.$router.replace('/home')
      })
    }
  },
  created() {
    //获取cookies中的值
    this.loginForm.username = this.$cookies.get('username')
    this.loginForm.password = this.$cookies.get('password')
  }
}
</script>

<style scoped>
.login-card {
  width: 450px;
  margin: auto;
  transform: translateY(50%);
}
.login-btn {
  width: 100%;
}
.login-btn .el-button {
  width: 50%;
}
</style>
